package br.com.q7solucoes.commons.wordpress;

import br.com.q7solucoes.commons.utils.StringUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Representa um post do wordpress.
 *
 * @author Elenilson Vieira - elenilson[at]q7solucoes.com.br
 * @since 9 de setembro de 2011
 * @version 1.0
 * @see www.q7solucoes.com.br
 */
public class WPPost {

    public enum WPPostCharset {
        LATIN1, UTF_8
    }
    
    private int id;
    private String title;
    private String content;
    private String link;
    private String image;
    private Date date;
    private List<String> categories = new ArrayList<String>();
    private List<String> tags = new ArrayList<String>();
    private WPPostCharset chartSet;

    /**
     * Construtor sem argumentos
     * 
     * @param chartSet a codificacao a ser utilizada
     */
    public WPPost(WPPostCharset chartSet) {
        this.chartSet = chartSet;
    }

    /**
     * Construtor com o link
     *
     * @param title o titulo
     * @param content o conteudo
     * @param link o link
     * @param image a imagem do post
     * @param date a data de publicacao
     * @param categories as categorias
     * @param tags as tags
     * @param chartSet a codificacao a ser utilizada
     */
    public WPPost(String title, String content, String link, String image, Date date, 
            List<String> categories, List<String> tags, WPPostCharset chartSet) {
        this.title = title;
        this.content = content;
        this.link = link;
        this.image = image;
        this.date = date;
        this.categories = categories;
        this.tags = tags;
        this.chartSet = chartSet;
    }

    /**
     * Construtor sem o link
     *
     * @param title o titulo
     * @param content o conteudo
     * @param image a imagem do post
     * @param date a data de publicacao
     * @param categories as categorias
     * @param tags as tags
     * @param chartSet a codificacao a ser utilizada
     */
    public WPPost(String title, String content, String image, Date date, 
            List<String> categories, List<String> tags, WPPostCharset chartSet) {
        this.title = title;
        this.content = content;
        this.image = image;
        this.date = date;
        this.categories = categories;
        this.tags = tags;
        this.chartSet = chartSet;
    }

    /**
     * Construtor sem argumentos
     */
    public WPPost() {
        this(WPPostCharset.LATIN1);
    }

    /**
     * Construtor com o link
     *
     * @param title o titulo
     * @param content o conteudo
     * @param link o link
     * @param image a imagem do post
     * @param date a data de publicacao
     * @param categories as categorias
     * @param tags as tags
     */
    public WPPost(String title, String content, String link, String image, Date date, List<String> categories, List<String> tags) {
        this(title, content, link, image, date, categories, tags, WPPostCharset.LATIN1);
    }

    /**
     * Construtor sem o link
     *
     * @param title o titulo
     * @param content o conteudo
     * @param image a imagem do post
     * @param date a data de publicacao
     * @param categories as categorias
     * @param tags as tags
     */
    public WPPost(String title, String content, String image, Date date, List<String> categories, List<String> tags) {
        this(title, content, image, date, categories, tags, WPPostCharset.LATIN1);
    }

    public List<String> getCategories() {
        switch(chartSet){
            case LATIN1: {
                List<String> categoriesLatin1 = new ArrayList<String>();
                for (String cat : categories) {
                    try{
                        categoriesLatin1.add(StringUtils.convertUTF8ToIso(cat));
                    }catch(Exception ex){
                        categoriesLatin1.add(cat);
                    }
                }
        
                return categoriesLatin1;
            }
                
            case UTF_8: {
                List<String> categoriesUtf8 = new ArrayList<String>();
                for (String cat : categories) {
                    try{
                        categoriesUtf8.add(StringUtils.convertIsoToUTF8(cat));
                    }catch(Exception ex){
                        categoriesUtf8.add(cat);
                    }
                }
        
                return categoriesUtf8;
            }
                
            default:return categories;
        }
    }

    public void setCategories(List<String> categories) {
        this.categories = categories;
    }

    public String getContent() {
        switch(chartSet){
            case LATIN1: {
                try{
                    return StringUtils.convertUTF8ToIso(content);
                }catch(Exception ex){
                    return content;
                }
            }
                
            case UTF_8: {
                try{
                    return StringUtils.convertIsoToUTF8(content);
                }catch(Exception ex){
                    return content;
                }
            }
                
            default:return content;
        }
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getLink() {
        if(link != null)
            return StringUtils.removeAccents(link).replace(" ", "-").replace("/", "").replace("---", "-").toLowerCase().trim();

        String titleWithoutPonctuation = StringUtils.removeAccents(title).replace(" ", "-");
        String dateString = StringUtils.converteDateToString(date).replace("/", "");

        return String.format("%s-%s", titleWithoutPonctuation, dateString).replace("---", "-").toLowerCase().trim();
    }

    public void setLink(String link) {
        this.link = link;
    }

    public List<String> getTags() {
        switch(chartSet){
            case LATIN1: {
                List<String> tagsLatin1 = new ArrayList<String>();
                for (String cat : tags) {
                    try{
                        tagsLatin1.add(StringUtils.convertUTF8ToIso(cat));
                    }catch(Exception ex){
                        tagsLatin1.add(cat);
                    }
                }
        
                return tagsLatin1;
            }
                
            case UTF_8: {
                List<String> tagsUtf8 = new ArrayList<String>();
                for (String cat : tags) {
                    try{
                        tagsUtf8.add(StringUtils.convertIsoToUTF8(cat));
                    }catch(Exception ex){
                        tagsUtf8.add(cat);
                    }
                }
        
                return tagsUtf8;
            }
                
            default:return tags;
        }
    }

    public void setTags(List<String> tags) {
        this.tags = tags;
    }

    public String getTitle() {
        switch(chartSet){
            case LATIN1: {
                try{
                    return StringUtils.convertUTF8ToIso(title);
                }catch(Exception ex){
                    return title;
                }
            }
                
            case UTF_8: {
                try{
                    return StringUtils.convertIsoToUTF8(title);
                }catch(Exception ex){
                    return title;
                }
            }
                
            default:return title;
        }
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public WPPostCharset getChartSet() {
        return chartSet;
    }

    @Override
    public String toString() {
        StringBuilder s = new StringBuilder("WPPost{" + "id=" + id + ", title=" + getTitle() + ", content=" + getContent() + 
                ", link=" + getLink() + ", image=" + getImage() + ", date=" + getDate() + ", chartSet=" + getChartSet() + '}');
        
        if(getCategories() != null){
            s.append("\nCategorias: ");
            for (String c : getCategories()) {
                s.append(c).append(", ");
            }
        }
        
        if(getTags() != null){
            s.append("\nTags: ");
            for (String c : getTags()) {
                s.append(c).append(", ");
            }
        }
        
        return s.toString();
    }   
    
}
